package com.sxc.oa.web.action;

import com.sxc.oa.utils.DButil;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class AddMemberServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        String deptno1 = request.getParameter("deptno1");
        String name = request.getParameter("name");
        String zhuanye = request.getParameter("zhuanye");
        String id = request.getParameter("id");

        Connection conn = null;
        PreparedStatement ps = null;

        try {
            conn = DButil.getConnction();
            String sql = "INSERT INTO sss (id,deptno1, name, zhuanye) VALUES (? ,?, ?, ?)";
            ps = conn.prepareStatement(sql);
            ps.setString(1,id);
            ps.setString(2, deptno1);
            ps.setString(3, name);
            ps.setString(4, zhuanye);
            ps.executeUpdate();

            response.sendRedirect(request.getContextPath() + "/success.html");
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            try {
                DButil.close(conn, ps, null);
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
    }
}